<template>
  <div class="search">
    <el-form
      class="search-form"
      ref="form"
      :inline="true"
      :model="formData"
      :label-suffix="$t('tip.colon')"
      size="small"
    >
      <slot v-bind:formData="formData">
        <el-form-item prop="keyword" :label="$t('tip.keyword')">
          <el-input
            v-model="formData.keyword"
            :placeholder="keywordPlaceholder"
          />
        </el-form-item>
      </slot>
    </el-form>
    <div class="search-btns">
      <el-button type="primary" size="small" @click="search">{{
        $t('action.search')
      }}</el-button>
      <el-button size="small" @click="reset">{{
        $t('action.reset')
      }}</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Search',
  props: {
    searchData: {
      type: Object,
      default: () => ({})
    },
    keywordPlaceholder: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {}
    }
  },
  methods: {
    search() {
      this.$emit('search', { ...this.formData })
    },
    reset() {
      this.formData = { ...this.defaultData }
      this.$refs.form.resetFields()
    }
  },
  mounted() {
    this.defaultData = { ...this.searchData }
    this.reset()
  }
}
</script>

<style lang="scss" scoped>
@import 'styles/common.scss';
.search {
  margin-bottom: $divider;
  padding: $barPadding;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: $colorWhite;
  border-radius: $borderRadius;
  .search-form {
    flex: 1;
    ::v-deep.el-form-item {
      margin-bottom: 0 !important;
    }
  }
  // .search-btns {
  //   margin-left: 0.1rem;
  // }
}
</style>
